package com.jt.assistant.ui

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.View
import com.blankj.utilcode.util.GsonUtils
import com.jt.assistant.R
import com.jt.assistant.base.GlobalConfig.SCAN_DATA
import com.jt.assistant.base.GlobalConfig.SCAN_TYPE_ONESHOT
import com.jt.assistant.bean.ScanDataBean
import com.jt.assistant.bean.Waybill
import com.jt.assistant.viewmodel.HomeViewModel
import com.orhanobut.logger.Logger
import com.yzp.mvvmlibrary.base.BaseActivity
import kotlinx.android.synthetic.main.activity_scan_home.*
import java.util.*

/**
 * 送仓扫码查询 JDV001502833734-15-0-
 */
class ScanHomeActivity : BaseActivity<HomeViewModel>() {

    private var mCartonCode = ""
    private var mScanData: ScanDataBean? = null


    override fun onDestroy() {
        set_scan.unregisterEventBus()

        super.onDestroy()
    }

    override fun layoutId(): Int = R.layout.activity_scan_home

    override fun initView(savedInstanceState: Bundle?) {
        set_scan.initCamera(this, SCAN_TYPE_ONESHOT)
    }

    override fun initData() {
    }


    @SuppressLint("SetTextI18n")
    override fun initListener() {
        set_scan.setOnScanListener {
            mCartonCode = it
            val map: MutableMap<String, String> = HashMap()
            map["cartonCode"] = it
            viewModel.getScanList(map)
        }

        bt_commit.setOnClickListener {
            startActivity(Intent(this@ScanHomeActivity, ScanActivity::class.java).apply {
                putExtra(SCAN_DATA, GsonUtils.toJson(mScanData))
                set_scan.clearData()
                tv_status.text = ""
                bt_commit.visibility = View.GONE
            })
        }//201008183562


        viewModel.mGetScanList.observe(this, {
            if (it.isSuccess()) {
                val states = resources.getStringArray(R.array.waybill_state_name)
                mScanData = it.data
                if (states[mScanData!!.state] == "待提货" || states[mScanData!!.state] == "已提货") {
                    if (findPosition() < 0) {
                        speakString("不在扫码状态")
                        return@observe
                    }
                    removeData()

                    tv_status.text =
                        "${set_scan.string}\n共有${mScanData!!.waybillList.size}个运单,  " +
                                "${mScanData!!.waybillList[0].cartonNum - mScanData!!.scannedList.size}箱待扫\n" +
                                "运单状态:${states[mScanData!!.state]}"
                    bt_commit.visibility =
                        if ((mScanData!!.waybillList[0].cartonNum - mScanData!!.scannedList.size) == 0) View.GONE else View.VISIBLE
                } else {
                    tv_status.text = "${set_scan.string}\n共有${mScanData!!.waybillList.size}个运单,  " +
                            "${mScanData!!.totalCarton - mScanData!!.scannedCarton}箱待扫\n" +
                            "运单状态:${states[mScanData!!.state]}"

                    bt_commit.visibility =
                        if (mScanData!!.totalCarton - mScanData!!.scannedCarton == 0) View.GONE else View.VISIBLE
                }
            } else {
                tv_status.text = ""
                bt_commit.visibility = View.GONE
            }
            set_scan.clearData()
        })

    }

    private fun findPosition(): Int {
        for (index in mScanData!!.waybillList.indices) {
            if (mCartonCode.contains(mScanData!!.waybillList[index].transCode + "")
                || mCartonCode.contains(mScanData!!.waybillList[index].waybillNo + "")
            ) {
                return index
            }
        }
        return -1
    }

    private fun removeData() {
        val waybill: Waybill = mScanData!!.waybillList[findPosition()]
        val scannedList: MutableList<String> = ArrayList()
        mScanData!!.waybillList.clear()
        mScanData!!.waybillList.add(waybill)


        for (i in mScanData!!.scannedList.indices) {
            val scanned: String = mScanData!!.scannedList[i]
            if (scanned.contains(waybill.transCode + "") || scanned.contains(waybill.waybillNo + "")) {
                scannedList.add(scanned)
            }
        }
        mScanData!!.scannedList.clear()
        mScanData!!.scannedList.addAll(scannedList)
    }

    //200922814469 200923580409  200924381587 200924381587  200924381587000002
    //201012140125  201012327502  201012140125000004 201012140125000005 //已入仓
    //201015641602   201015819878  201015641602000003
    //200924381587000002


}